package com.zj.leetcode.medium.tree;

import java.util.LinkedList;
import java.util.Queue;

/**
 * @program: algorithm
 * @description: 找树左下角的值
 * 513 find-bottom-left-tree-value
 * @author: Zhang Bo
 * @create: 2022-06-13 17:58:34
 **/
public class FindBottomLeftTreeValue {
    public static void main(String[] args) {
        Solution solution = new FindBottomLeftTreeValue().new Solution();
    }
    //leetcode submit region begin(Prohibit modification and deletion)

    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    class Solution {
        /**
         * [3,1,5,0,2,4,6]
         *
         * @param root
         * @return
         */
        public int findBottomLeftValue(TreeNode root) {
            int res = root.val;
            Queue<TreeNode> queue = new LinkedList<>();
            queue.add(root);

            while (queue.size() > 0) {

                TreeNode temp = queue.poll();
                if (temp == null) {
                    continue;
                }
                if (temp.right != null) {
                    res = temp.right.val;
                    queue.add(temp.right);
                }

                if (temp.left != null) {
                    res = temp.left.val;
                    queue.add(temp.left);
                }
            }
            return res;

        }
    }
//leetcode submit region end(Prohibit modification and deletion)

}